package pers.qianyu.month_202101.date_20210130;

/**
 * 78. 左旋转字符串
 * https://www.acwing.com/problem/content/74/
 *
 * @author mizzle rain
 * @date 2021年1月30日16:09:56
 */
public class LeftRotateString {
    public String leftRotateString(String str, int n) {
        char[] arr = str.toCharArray();
        reverse(arr, 0, arr.length - 1);
        reverse(arr, 0, arr.length - n - 1);
        reverse(arr, arr.length - n, arr.length - 1);
        return new String(arr);
    }

    private void reverse(char[] arr, int left, int right) {
        for (int i = left, j = right; i < j; i++, j--) {
            char ch = arr[i];
            arr[i] = arr[j];
            arr[j] = ch;
        }
    }
}